package cn.imokay.juc.threadshutdown;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanDemo {
    static AtomicBoolean stoped = new AtomicBoolean(false);
    public static void main(String[] args) {
        {
            new Thread(()->{
                while (true) {
                    if (stoped.get()) {
                        System.out.println(Thread.currentThread().getName() + "\t---AtomicBoolean为ture,被协商终止---");
                        break;
                    }
                    System.out.println(Thread.currentThread().getName() + "\t---hello,AtomicBoolean---");
                }
            },"t1").start();

            try { TimeUnit.SECONDS.sleep(1); } catch (Exception e) { e.printStackTrace();}

            new Thread(()->{
                stoped.set(true);
            },"t2").start();
        }
    }
}
